define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    import_url: 'order/import',
                    table: 'order',
                }
            });
            var courseList = {"0":__('Course.Type 0'),"1":__('Course.Type 1'),"2":__('Course.Type 2'),"3":__('Course.Type 3')};
            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'venue.name', title: __('所属场馆'),operate:false},
                        {field: 'venue_id', title: __('所属场馆'),addclass: 'selectpage', extend:'data-source="ajax/venue"', defaultValue:Config.vid, visible:false,},
                        {field: 'order_no', title: __('Order_no')},
                        {field: 'user.username', title: __('用户名'), operate: 'LIKE'},
                        {field: 'user_id', title: __('学员id'),visible:false,},
                        {field: 'course_id', title: __('课程名称'),addclass: 'selectpage', extend:'data-source="course/index"', visible:false,},
                        {field: 'course.name', title: __('课程名称'), operate: 'LIKE'},
                        {field: 'snapshot', title: __('Snapshot'), operate: 'LIKE'},
                        {field: 'ctime', title: __('Ctime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        //{field: 'term', title: __('Price') ,visible:false,operate:false},
                        {field: 'course.type', title: __('课程类型'), searchList: courseList,
                            formatter:function(value,row,index){
                                return courseList[value];
                            }
                        },
                        {field: 'from', title: __('From'), searchList: {"0":__('From 0'),"1":__('From 1')}, formatter: Table.api.formatter.normal},
                        //{field: 'admin_id', title: __('Admin_id')},
                        {field: 'admin.username', title: __('Admin_id'), operate: 'LIKE'},
                        {field: 'pay_type', title: __('Pay_type'), searchList: {"0":__('Pay_type 0'),"1":__('Pay_type 1'),"2":__('Pay_type 2'),"3":__('Pay_type 3')}, formatter: Table.api.formatter.normal},
                        {field: 'proof_images', title: __('Proof_images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'ptime', title: __('Ptime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1')}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        {field: 'remarks', title: __('Remarks'), operate: 'LIKE'},
                        /*{field: 'ext', title: __('Ext'), operate: 'LIKE'},*/
                        {field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name:'',
                                    text:'',
                                    title: __('团体课的预订详情'),
                                    icon: 'fa fa-trash',
                                    classname: 'btn btn-xs btn-danger btn-delone',
                                    confirm: '确认删除吗？',
                                    url: 'order/del',
                                    hidden:function(row){
                                        if(row.from != 1 || row.status == 3){
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name:'团体单',
                                    text:'预订',
                                    title: __('团体课需要后台预订'),
                                    icon: 'fa fa-calendar',
                                    classname: 'btn btn-xs btn-success  btn-dialog',
                                    width: '130px',
                                    high: '130px',
                                    url: 'order/plan',
                                    hidden:function(row){
                                        if(row.course.type !=3 || row.status !=1 || row.term){
                                            return true;
                                        }
                                    }
                                },
                                {
                                    name:'团体单',
                                    text:'取消预订',
                                    title: __('团体课只能后台取消预订'),
                                    icon: 'fa fa-times',
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    url: 'order/changePlan',
                                    hidden:function(row){
                                        if(row.course.type !=3 || row.status !=1 ||  !row.term || row.term.type != 1){
                                            return true;
                                        }
                                    },
                                    refresh:true
                                },
                                {
                                    name:'团体单',
                                    text:'预约详情',
                                    title: __('团体课的预订详情'),
                                    icon: 'fa fa-info-circle',
                                    classname: 'btn btn-xs btn-info btn-addtabs',
                                    url: 'user_course/index?status=0&term_id={term.term_id}',
                                    hidden:function(row){
                                        if(row.course.type !=3 || row.status !=1 ||  !row.term || !row.term.id){
                                            return true;
                                        }
                                    }
                                }

                            ],
                            formatter: Table.api.formatter.operate}

                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $('#c-phone_id').change(function () {
                var phone = $('#c-phone_id').val();
                $("#c-user_id").val('');
                $.ajax({
                    async: false,
                    url: 'ajax/userInfo',
                    type: 'get',
                    data: {custom: {mobile: phone}},
                    dataType: 'json',
                    success: function (res) {
                        if (res) {
                            $("#c-user_id").val(res.username);
                        } else {
                            Toastr.error("未查到该用户");
                        }

                    }
                });
            });

            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();

        },
        plan: function () {
            $("#c-user_ids").data("params", function (obj) {
                return {custom: {uid: $("#c-user_id").val()}};
            });
            Controller.api.bindevent();

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});